package com.wfm.mianshi;

import java.util.Arrays;

//
//给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数
//
//        测试用例：
//
//        nums = [1,2,3,4,5,6,7], k = 3，结果[5,6,7,1,2,3,4]
//        nums = [1,2,3], k = 4，结果[3,1,2]
public class Demo4 {
    public static int[] rotate(int[] nums,int k){
        int n = nums.length;
        k=k%n;
        int[] rotated = new int[n];
        for (int i = 0; i < n; i++) {
            rotated[(i+k)%n]=nums[i];
        }
        for (int i = 0; i < n; i++) {
            nums[i]=rotated[i];
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 4, 5, 6, 7};
        int k = 3;
        int[] rotate = rotate(ints, k);
        System.out.println(Arrays.toString(rotate));


        int[] ints2 = {1,2,3};
        int k2 = 4;
        int[] rotate2 = rotate(ints2, k2);
        System.out.println(Arrays.toString(rotate2));
    }
}
